<script>
  var country_id = 0, state_id = 0;
  jQuery(document).ready(function(){
    $('#country_id').bind({
      change: function (){detect_country($(this).val());},
      blur: function (){detect_country($(this).val());}
    });  
  $('#state_id').bind({
      change: function (){detect_state($(this).val());},
      blur: function (){detect_state($(this).val());}
    });      
    detect_country($('#country_id').val());      
  });
  function detect_country(id){
    if(country_id != id){
      country_id = id;
      $('#state_id').html('<option value=""><?php echo $this->lang->line('label.option.select') ?></option>');
      if(id){        
        $.post('<?php echo site_url('admin/ajax/list_json_state') ?>?id=' + id, {}, function(data){
          if(data){
            $.each(data, function(key, value){
              $('#state_id').append('<option '+
                (value['id'] == '<?php echo isset($obj['state_id']) ? $obj['state_id'] : 0 ?>' ? 'selected="selected"' :'' )
                +' value="'+value['id']+'">'+ value['name'] +'</option>');
              if(value['id'] == '<?php echo isset($obj['state_id']) ? $obj['state_id'] : 0 ?>'){
                detect_state(value['id']);
              }
            })
          }
        }, 'json');
      }
    }
  }
  
  function detect_state(id){
    if(state_id != id){
      state_id = id;
      $('#city_id').html('<option value=""><?php echo $this->lang->line('label.option.select') ?></option>');
      if(id){
        $.post('<?php echo site_url('admin/ajax/list_json_city') ?>?id=' + id, {}, function(data){
          if(data){
            $.each(data, function(key, value){
              $('#city_id').append('<option '+
                (value['id'] == '<?php echo isset($obj['city_id']) ? $obj['city_id'] : 0 ?>' ? 'selected="selected"' :'' )
                +' value="'+value['id']+'">'+ value['name'] +'</option>');
            })
          }
        }, 'json');
      }
    }
  }
</script>